$(function() {
    fillLine();
    createDatepickers();
    pageLanguage();
    changeLanguage();
});

/**
 * user check language - send request to server (ajax)
 * after response - reload page;
 * @returns {undefined}
 */
function changeLanguage() {
    $('.menu_language ul li').on('click', function() {
        window.location.assign(urlBackend + 'language/lang/' + $(this).attr('value'));
//        $.ajax({
//            type: 'POST', 
//            url: urlBackend + 'language',
//            data: {
//                language: $(this).attr('value')
//            },
//            success: function(msg) {
//                window.location.reload();
//            }
//        });
    });
}

/**
 * selected current language
 * @returns {undefined}
 */
function pageLanguage() {
    $('.menu_language ul li').each(function(i, v) {
       if ($(v).attr('value') === language) {
           $(v).addClass('selected');
       } 
    });
}

/**
 * add to all fields choice date - jquery datepicker
 * and click listener to image in field choice date - open jquery datepicker
 */
function createDatepickers() {
    $('.ui_datapicer_input_from').datepicker({
        dateFormat: 'yy-mm-dd',
        onClose: function(selectedDate) {
            $('.ui_datapicer_input_to').datepicker('option', 'minDate', selectedDate);
        }
    });
    
    $('.ui_datapicer_input_to').datepicker({
        dateFormat: 'yy-mm-dd'
    });
    
    $('.ui_datapicer_img_from').on('click', function() {
        $('.ui_datapicer_input_from').datepicker('show');
    });
    
    $('.ui_datapicer_img_to').on('click', function() {
        $('.ui_datapicer_input_to').datepicker('show');
    });
    
    return false;
}

/**
 * for all line add color 
 */
function fillLine() {
    $('.ui_one_line.has_fill').each(function(i, v) {
        if (i % 2 === 0) {
            $(this).css({
                'background-color': '#ededed'
            });
        } else {
            $(this).css({
                'background-color': '#ffffff'
            });
        }
    });
    
    return false;
}

/**
 * get current date in format YY-mm-dd
 * @returns {String date}
 */
function getCurrentDate() {
    var d = new Date();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var output = d.getFullYear() + '-' +
            (month < 10 ? '0' : '') + month + '-' +
            (day < 10 ? '0' : '') + day;

    return output;
}

/**
 * return sum date plus days
 * @param {type} night
 * @param {type} date
 * @returns {String date}
 */
function getOutDate(night, date) {
    var dateArray = date.split("-");
    var d = new Date(dateArray[0], parseInt(dateArray[1], 10) - 1, dateArray[2]);
    var end = new Date(d.getTime() + (night * 24 * 60 * 60 * 1000));
    var output = end.getFullYear() + '-' +
            ((end.getMonth() + 1) < 10 ? '0' : '') + (end.getMonth() + 1) + '-' +
            (end.getDate() < 10 ? '0' : '') + end.getDate();

    return output;
}


